package com.xiaobing.user.controller;

import com.xiaobing.common.base.BaseController;
import com.xiaobing.common.constant.UserConstants;
import com.xiaobing.common.entity.system.SysPermissionMenu;
import com.xiaobing.common.res.Result;
import com.xiaobing.common.utils.StringUtils;
import com.xiaobing.user.service.ISysPermissionMenuService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 菜单信息
 *
 * @author xulk
 */
@RestController
@RequestMapping("/user/menu")
public class SysPermissionMenuController extends BaseController {

    @Resource
    private ISysPermissionMenuService menuService;

    /**
     * 获取菜单列表
     */
    @GetMapping("/list")
    public Result list(SysPermissionMenu menu) {
        List<SysPermissionMenu> menus = menuService.selectMenuList(menu, getUserId());
        return Result.success(menus);
    }

    /**
     * 根据菜单编号获取详细信息
     */
    @GetMapping(value = "/{menuId}")
    public Result getInfo(@PathVariable Long menuId) {
        return Result.success(menuService.selectMenuById(menuId));
    }

    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/treeSelect")
    public Result treeselect(SysPermissionMenu menu) {
        List<SysPermissionMenu> menus = menuService.selectDeptToTree(menu, getUserId());
        return Result.success(menus);
    }


    /**
     * 新增菜单
     */
    @PostMapping
    public Result add(@Validated @RequestBody SysPermissionMenu menu) {
        if (!menuService.checkMenuNameUnique(menu)) {
            return error("新增菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        } else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) && !StringUtils.ishttp(menu.getPath())) {
            return error("新增菜单'" + menu.getMenuName() + "'失败，地址必须以http(s)://开头");
        }
        return Result.success(menuService.insertMenu(menu));
    }

    /**
     * 修改菜单
     */
    @PutMapping
    public Result edit(@Validated @RequestBody SysPermissionMenu menu) {
        if (!menuService.checkMenuNameUnique(menu)) {
            return error("修改菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        } else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) && !StringUtils.ishttp(menu.getPath())) {
            return error("修改菜单'" + menu.getMenuName() + "'失败，地址必须以http(s)://开头");
        } else if (menu.getId().equals(menu.getParentId())) {
            return error("修改菜单'" + menu.getMenuName() + "'失败，上级菜单不能选择自己");
        }
        return Result.success(menuService.updateMenu(menu));
    }

    /**
     * 删除菜单
     */
    @DeleteMapping("/{menuId}")
    public Result remove(@PathVariable("menuId") Long menuId) {
        if (menuService.hasChildByMenuId(menuId)) {
            return Result.error("存在子菜单,不允许删除");
        }
        if (menuService.checkMenuExistRole(menuId)) {
            return Result.error("菜单已分配,不允许删除");
        }
        return Result.success(menuService.deleteMenuById(menuId));
    }
}